package com.xinke.juc.case04;

import com.xinke.juc.util.Sleeper;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author：xinke
 * @Date：2025/10/3
 * @Desc:
 */
@Slf4j
public class WaitNotify04 {
    static final Object room = new Object();
    static boolean hasCigarette = false;
    static boolean hasTakeout = false;

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (room) {
                log.info("有烟没？【{}】", hasCigarette);
                if (!hasCigarette) {
                    log.info("没烟，先歇会！");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.info("有烟没？【{}】", hasCigarette);
                if (hasCigarette) {
                    log.info("可以开始干活了");

                } else {
                    log.info("没干成活。。。");
                }
            }
        }, "小南").start();

        new Thread(() -> {
            synchronized (room) {
                Thread thread = Thread.currentThread();
                log.info("外卖送到没？【{}】", hasTakeout);
                if (!hasTakeout) {
                    log.info("没外卖，先歇会！");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.info("外卖送到没？【{}】", hasTakeout);
                if (hasTakeout) {
                    log.info("可以开始干活了");
                } else {
                    log.info("没干成活。。。");
                }
            }
        }, "小女").start();
        Sleeper.sleep(1);
        new Thread(() -> {
            synchronized (room) {
                hasTakeout = true;
                log.info("烟到了噢。。。");
//                room.notify();
                room.notifyAll();
            }
        }, "送烟的").start();
    }
}
